//
//  ArcView.m
//  ConuntStep
//
//  Created by dllo on 16/4/19.
//  Copyright © 2016年 C. All rights reserved.
//

#import "ArcView.h"


@implementation ArcView
//相关扩展http://blog.csdn.net/lvxiangan/article/details/23618553
//覆盖drawRect方法，你可以在此自定义绘画和动画
-(void)drawRect:(CGRect)rect{
    // 仪表盘底部
    drawHu1();
    //仪表盘
    [self drawHu2];
    //仪表盘进度
    [self drawHu3];

}
void drawHu1(){

    //1 获取上下文  扩展http://blog.csdn.net/rhljiayou/article/details/9919713
    //一个不透明类型的Quartz 2D绘画环境，相当于一个画布，你可以在上面任意绘画
    CGContextRef context = UIGraphicsGetCurrentContext();
    //1.1设置线条的宽度
    CGContextSetLineWidth(context, 1);
    //1.2设置线条的起始点样式
    CGContextSetLineCap(context, kCGLineCapRound);
    //1.3画笔线的颜色
//    CGContextSetRGBStrokeColor(context, 1, 1, 1, 1.0);
    //1.3虚实切换,绘制10 跳过5
    //1.4设置颜色
   [[[UIColor alloc]initWithRed:0.0/255.0 green:201.0/255.0 blue:87.0/255.0 alpha:1] set];
    //2.设置路径
    CGContextAddArc(context, kScreenWidth / 2, kScreenWidth / 2, 120, -M_PI, M_PI, 0);
    //绘制
    CGContextStrokePath(context);
    
}
-(void)drawHu2{
    //1.获取上下文
    CGContextRef contex = UIGraphicsGetCurrentContext();
    //1.1设置线条的宽度
    CGContextSetLineWidth(contex, 18);
    //1.2设置线条的起始点样式   http://my.oschina.net/227/blog/387122
    CGContextSetLineCap(contex, kCGLineCapButt);
    //1.3虚实切换
    CGFloat length[] = {4,2};//表示先绘制4个点,再跳过2个点,如此反复
    //phase稍后再说 length指明虚线是如何交替绘制,具体看例子http://www.docin.com/p-1256184017.html
    //count - lengths数组的长度
    CGContextSetLineDash(contex, 0, length, 2);
    //1.4 设置颜色
    [[[UIColor alloc]initWithRed:225.0/255.0 green:225.0/255.0 blue:225.0/255.0 alpha:1] set];
    //2.设置路径
    CGContextAddArc(contex, kScreenWidth / 2, kScreenWidth / 2, 80, 1.5 * M_PI, 3.5 * M_PI, 0);
    //2.1绘制
    CGContextStrokePath(contex);
}
-(void)drawHu3{
    //1.获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //1.1设置线条宽度
    CGContextSetLineWidth(context, 18);
    //1.2设置线条的起始点样式
    CGContextSetLineCap(context, kCGLineCapButt);
    //1.3虚实切换
    CGFloat length[] = {4,2};
    CGContextSetLineDash(context, 0, length, 2);
    //1.4设置颜色
    [[[UIColor alloc]initWithRed:0.0/255.0 green:201.0/255.0 blue:87.0/255.0 alpha:1] set];
    //2.设置路径
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(numberChange:) name:@"number" object:nil];
    CGFloat end = 1.5 * M_PI + (2 * M_PI * self.num / 5000);
    CGContextAddArc(context, kScreenWidth / 2, kScreenWidth / 2, 80, 1.5 * M_PI, end, 0);
    //3.绘制图
    CGContextStrokePath(context);
}
-(void)numberChange:(NSNotification *)text
{
    self.num =[text.userInfo[@"num"] intValue];
    [self setNeedsDisplay];
}
-(void)setNum:(NSInteger)num{
    _num = num;
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        
        
        UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 120, 30)];
        lab2.text =@"单位：步（step）";
        lab2.font = [UIFont systemFontOfSize:13];
        
        
        UILabel *la1 = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth - 120) / 2, (kScreenWidth - 80) / 2, 120, 30)];
        la1.text = @"步数";
        la1.textAlignment = NSTextAlignmentCenter;
        
        
        _numLabel=[[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth - 120)/2, ( kScreenWidth - 80) / 2 + 30, 120, 30)];
        _numLabel.textAlignment =NSTextAlignmentCenter;
        _numLabel.textColor=[UIColor blackColor];
        _numLabel.text = @"0步";
        _numLabel.font=[UIFont systemFontOfSize:18];
        if (!_timer) {
            _timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(change) userInfo:nil repeats:YES];
            
        }
        
        UILabel *la3 = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth - 120) / 2, (kScreenWidth - 80) /2 + 60, 120, 30)];
        
        
        la3.text = @"每日目标5000步";
        la3.textAlignment = NSTextAlignmentCenter;
        la3.font=[UIFont systemFontOfSize:12];
        
        
        [self addSubview:lab2];
        [self addSubview:_numLabel];
        [self addSubview:la1];
        [self addSubview:la3];
    }
    return self;
}
-(void)change
{    _numLabel.text =[NSString stringWithFormat:@"%ld步",(long)_num];
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:_numLabel.text,@"num", nil];
    
    // 创建通知
    NSNotification *noti =[NSNotification notificationWithName:@"number" object:nil userInfo:dic];
    
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotification:noti];
}




@end
